﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;


namespace 李晓霞web
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
       
        }

        protected void Quit_Click(object sender, EventArgs e)
        {

            this.Dispose();
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string strCn = "server=pc.hstc.cn;database=dhd2014112106;uid=2014112106;password=19950616";
            SqlConnection cn = new SqlConnection(strCn);

            try
            {
                if (this.rdoTeacher.Checked)
                {
                    cn.Open();
                    string strSql = "SELECT * FROM TeacherRegister where UserName =N'" + this.txtUserName.Text + "'";
                    SqlCommand cmd = new SqlCommand(strSql, cn);
                    SqlDataReader c = cmd.ExecuteReader();
                    string password = "";
                    //string Sname = ""; 
                    if (c.Read())
                    {
                        password = (string)c.GetValue(1);
                        string checkcode = Session["CheckCode"].ToString();
                        if (password == txtUserPwd.Text)
                        {
                           
                        
                            Session["UserName"] = this.txtUserName.Text;
                            Session["Password"] = this.txtUserPwd.Text;
                            string name;
                            name = Session["UserName"].ToString();
                            if (this.TextBox1.Text == checkcode)
                            {
                                Response.Write("<script>alert('欢迎" + name + "老师进入学生信息系统!');window.location.href ='TeacherMainPage.aspx'</script>");
                            }
                            else
                            {
                                ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('验证码输入错误!')", true);
                            }



                            
                        }
                        else
                        {
                           
                            Response.Write("<script>alert('密码错误，请重新输入！');</script>");
                        }

                    }
                    else
                    {
                        Response.Write("<script>alert('不好意思,此用户不存在！');</script>");
                    }
                    c.Close();

                }
                else
                {
                    if (this.rdoStudent.Checked)
                    {
                        cn.Open();
                        string strSql = "SELECT * FROM StudentRegister where UserName =N'" + this.txtUserName.Text + "'";
                        SqlCommand cmd = new SqlCommand(strSql, cn);
                        SqlDataReader c = cmd.ExecuteReader();
                        string password = "";
                       
                        if (c.Read())
                        {
                            password = (string)c.GetValue(1);
                            string checkcode = Session["CheckCode"].ToString();
                            if (password == txtUserPwd.Text)
                            {
                                Session["UserName"] = this.txtUserName.Text;
                                Session["Password"] = this.txtUserPwd.Text;
                                string name;
                                name = Session["UserName"].ToString();
                                if (this.TextBox1.Text == checkcode)
                                {
                                    Response.Write("<script>alert('欢迎" + name + "同学进入学生信息系统!');window.location.href ='StudentMainPage.aspx'</script>");
                                }
                                else
                                {
                                    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('验证码输入错误!')", true);
                                }

                               
                            }
                            else
                            {
                                Response.Write("<script>alert('密码错误，请重新输入！');</script>");
                            }

                        }
                        else
                        {
                           
                            Response.Write("<script>alert('不好意思,此用户不存在！');</script>");
                        }
                        c.Close();


                    }

                }
            }
            catch
            {
                Response.Write("<script>alert('连接失败！');</script>");
            }
            finally
            {
                cn.Close();
            }
        
        }

        protected void Register_Click(object sender, EventArgs e)
        {
            Response.Redirect("Register.aspx");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }
    }
}